﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;

public partial class Sanpham_hoatdongsx : System.Web.UI.Page
{
    public int i = 1;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RadGrid_hdsx_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
    {
        GridEditableItem item = (GridEditableItem)e.Item;
        String id = item.GetDataKeyValue("mahd").ToString();

        if (e.Exception != null)
        {
            e.KeepInEditMode = true;
            e.ExceptionHandled = true;
            SetMessage("hdsx with ID " + id + " cannot be updated. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("hdsx with ID " + id + " is updated!");
        }
    }

    protected void RadGrid_hdsx_ItemInserted(object source, GridInsertedEventArgs e)
    {
        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            e.KeepInInsertMode = true;
            DisplayMessage(true, "hdsx cannot be inserted. Reason: " + e.Exception.Message);
        }
        else
        {
            DisplayMessage(false, "hdsx inserted");
        }
    }

    protected void RadGrid_hdsx_ItemDeleted(object source, GridDeletedEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        String id = dataItem.GetDataKeyValue("mahd").ToString();

        if (e.Exception != null)
        {
            e.ExceptionHandled = true;
            SetMessage("hdsx with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message);
        }
        else
        {
            SetMessage("hdsx with ID " + id + " is deleted!");
        }
    }

    private void DisplayMessage(string text)
    {
        RadGrid_hdsx.Controls.Add(new LiteralControl(string.Format("<span style='color:red'>{0}</span>", text)));
    }

    private void SetMessage(string message)
    {
        gridMessage = message;
    }

    private string gridMessage = null;
    protected void RadGrid_hdsx_DataBound(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(gridMessage))
        {
            DisplayMessage(gridMessage);
        }
    }
    private void DisplayMessage(bool isError, string text)
    {
        Label label = (isError) ? this.Label1 : this.Label2;
        label.Text = text;
    }
}
